package com.example.luke.shengdangxiaoattendance.Database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;


import com.example.luke.shengdangxiaoattendance.Model.Course;
import com.example.luke.shengdangxiaoattendance.Model.Setting;
import com.example.luke.shengdangxiaoattendance.Model.Student;
import com.example.luke.shengdangxiaoattendance.Model.StudentAttendance;
import com.example.luke.shengdangxiaoattendance.Util.GlobalFunction;

import java.util.ArrayList;
import java.util.Date;

/**
 * Created by luke on 9/21/16.
 */
public class DingHanDB extends SQLiteOpenHelper {
    public static final String DATABASE_NAME = "DingHan.db";

   private static final int DATABASE_VERSION = 1;
  private static DingHanDB dingHanDB=null;



    public static DingHanDB getInstance(Context context){
        if (dingHanDB==null)
            dingHanDB=new DingHanDB(context);

        return dingHanDB;

    }
   public DingHanDB(Context context){

      super(context,DATABASE_NAME,null,DATABASE_VERSION);


  }

    @Override
    public void onCreate(SQLiteDatabase db) {

        db.execSQL(Course.DH_CREATE_TABLE);
        db.execSQL(Student.DH_CREATE_TABLE);


        db.execSQL(StudentAttendance.DH_CREATE_TABLE);

        GlobalFunction.ToastAlert(GlobalFunction.context,db.getPath());
      //  db.execSQL(DH_CREATE_TABLE);
//        db.execSQL(PurchasedItemTB.DH_CREATE_TABLE);
//        db.execSQL(RefundGoodTB.DH_CREATE_TABLE);
//        db.execSQL(MenuItemTB.DH_CREATE_TABLE);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + Course.DH_TABLE_NAME);
        db.execSQL("DROP TABLE IF EXISTS " + Student.DH_TABLE_NAME);
        db.execSQL("DROP TABLE IF EXISTS " + StudentAttendance.DH_TABLE_NAME);

//        db.execSQL("DROP TABLE IF EXISTS " + PurchasedItemTB.DH_TABLE_NAME);
//        db.execSQL("DROP TABLE IF EXISTS " + RefundGoodTB.DH_TABLE_NAME);
//        db.execSQL("DROP TABLE IF EXISTS " + MenuItemTB.DH_TABLE_NAME);

        onCreate(db);
        db.execSQL(Student.DH_CREATE_INDEX);

    }

  private ContentValues contentValuesFromObj(Object object) {

      ContentValues contentValues = new ContentValues();
//
      if (object.getClass() == Course.class) {

           Course course=(Course)object;

          contentValues.put(Course.course_Time,course.Time);
          contentValues.put(Course.course_name_1,course.name1);
          contentValues.put(Course.course_name_2,course.name2);
          contentValues.put(Course.course_teacher,course.teacher);
          contentValues.put(Course.course_class,course.className);
          contentValues.put(Course.course_dateTime,course.dateTime);

      }else if(object.getClass()==Student.class){


            Student student=(Student) object;

          contentValues.put(Student.student_class,student.className);
          contentValues.put(Student.student_name,student.name);
          contentValues.put(Student.student_employeeCode,student.employeeID);
          contentValues.put(Student.student_id,student.studentID);
          contentValues.put(Student.student_unratedKW,student.unratedKW);
          contentValues.put(Student.student_dateTime,student.dateTime);
          contentValues.put(Student.student_kwName,student.kwName);

      }else if(object.getClass()==StudentAttendance.class){

          StudentAttendance student=(StudentAttendance) object;
          contentValues.put(Student.student_class,student.className);
          contentValues.put(Student.student_name,student.name);
          contentValues.put(StudentAttendance.attendance_date,student.attendanceDate);
          contentValues.put(StudentAttendance.attendance_time,student.attendanceTime);
          contentValues.put(StudentAttendance.student_employeeCode,student.employeeID);
          contentValues.put(StudentAttendance.class_time,student.classTime);
          contentValues.put(StudentAttendance.attendance_isPush,student.isPush);
          contentValues.put(StudentAttendance.student_remark,student.remark);

      }
//            Good record=(Good) object;
//            contentValues.put(Good.item_barcode,record.getBarcode());
//            contentValues.put(Good.item_discountable,record.getDisoountable());
//            contentValues.put(Good.item_id,record.getId());
//            contentValues.put(Good.item_kind,record.getKind());
//            contentValues.put(Good.item_name,record.getName());
//            contentValues.put(Good.item_pinyinID,record.getPinyinID());
//            contentValues.put(Good.item_priceout,record.getPriceOut());
//            contentValues.put(Good.item_priceIn,record.getPriceIn());
//            contentValues.put(Good.item_warning,record.getWarning());
//            contentValues.put(Good.item_storedNumber,record.getStoredNumber());


//        }else if(object.getClass()==GoodType.class){
//            GoodType record=(GoodType) object;
//            contentValues.put(GoodType.item_id,record.getId());
//            contentValues.put(GoodType.item_name,record.getName());
//
//        }else if (object.getClass()== PurchaseReport.class){
//
//            PurchaseReport record=(PurchaseReport) object;
//            contentValues.put(PurchaseReport.item_ticketID,record.getTicketID());
//            contentValues.put(PurchaseReport.item_commodityID,record.getCommodityID());
//            contentValues.put(PurchaseReport.item_commodityName,record.getCommodityName());
//            contentValues.put(PurchaseReport.item_volume,record.getVolume());
//            contentValues.put(PurchaseReport.item_primeCost,record.getPrimeCost());
//            contentValues.put(PurchaseReport.item_saleCost,record.getSaleCost());
//            contentValues.put(PurchaseReport.item_cost,record.getCost());
//            contentValues.put(PurchaseReport.item_restoredNum,record.getRestoredNum());
//            contentValues.put(PurchaseReport.item_discount,record.getDisCount());
//            contentValues.put(PurchaseReport.item_dateTime,record.getDateTime());
//            contentValues.put(PurchaseReport.item_dateTimeCopy,record.getDateTimeCopy());
//            contentValues.put(PurchaseReport.item_employeeID,record.getEmployeeID());
//            contentValues.put(PurchaseReport.item_payMode,record.getPayMode());
//            contentValues.put(PurchaseReport.item_uploadMark,record.getUploadMark());
//
//
//        }else if(object.getClass()==Operator.class){
//
//            Operator operator=(Operator)object;
//            contentValues.put(Operator.operator_operCardNo,operator.getOperCardNo());
//            contentValues.put(Operator.operator_employeeID,operator.getEmployeeID());
//            contentValues.put(Operator.operator_password,operator.getPassword());
//            contentValues.put(Operator.operator_authority,operator.getAuthority());
//
//        }


          return contentValues;


  }
    public void insert(String tableName, Object object){

        try {

            SQLiteDatabase db=this.getWritableDatabase();

            db.beginTransaction();

            db.insert(tableName, null, contentValuesFromObj(object));

            db.setTransactionSuccessful();
            db.endTransaction();
      //      db.close();



        }catch (Exception e){

            GlobalFunction.printTextByTag("insert fail:"+e.getMessage());
            e.printStackTrace();


        }

    }









    public ArrayList<Course> getAllCourseList(){
        ArrayList<Course> courseArrayList=new ArrayList<>();



        SQLiteDatabase db=this.getReadableDatabase();

        Cursor res=db.rawQuery("select * from "+Course.DH_TABLE_NAME +" WHERE "+Course.course_dateTime+"=?",new String[]{GlobalFunction.convertDate(new Date(),"yyyy-MM-dd")});
        res.moveToFirst();

        while (!res.isAfterLast()){
            Course course=new Course();

            course.Time= res.getString(res.getColumnIndex(Course.course_Time));
            course.name1= res.getString(res.getColumnIndex(Course.course_name_1));
            course.name2= res.getString(res.getColumnIndex(Course.course_name_2));
            course.teacher= res.getString(res.getColumnIndex(Course.course_teacher));
            course.className= res.getString(res.getColumnIndex(Course.course_class));

            courseArrayList.add(course);

            res.moveToNext();

        }


        res.close();


        return courseArrayList;


    }

    public int getStudentCountForClass(String className){

        SQLiteDatabase db=this.getReadableDatabase();

        Cursor res=        db.rawQuery("SELECT * from "+Student.DH_TABLE_NAME+" WHERE "+Student.student_class+"=? ",new String[]{className});

            //    db.query(Student.DH_TABLE_NAME,null,Student.student_class+"=?" +" AND "+Student.student_dateTime+"=?" ,new String[]{className,GlobalFunction.convertDate(new Date(),"yyyy-MM-dd")},null,null,null);

   // GlobalFunction.printTextByTag("retrieve:"+GlobalFunction.convertDate(new Date(),"yyyy-MM-dd"));

        int count=res.getCount();


        res.close();

        return count;


    }
    public void clearAllStudent(String date){

        SQLiteDatabase db=this.getReadableDatabase();

        Cursor res=db.rawQuery("DELETE from "+Student.DH_TABLE_NAME+" WHERE "+Student.student_dateTime+"=?",new String[]{date});

        if (res.moveToFirst()){
        GlobalFunction.printTextByTag("student list cleared");
        }
        res.close();


    }

    public   boolean updatePushMark(StudentAttendance studentAttendance){//更新长裤商品数量
        SQLiteDatabase db=this.getWritableDatabase();




        ContentValues cv=new ContentValues();
        cv.put(StudentAttendance.attendance_isPush,"1");

      int i=db.update(StudentAttendance.DH_TABLE_NAME,cv,StudentAttendance.student_employeeCode+"=?",new String[]{studentAttendance.employeeID});

      db.close();

        return i!=-1;


    }

    public void clearAttendanceList(){

//        SQLiteDatabase db=this.getReadableDatabase();
//
//        Cursor res=db.rawQuery("DELETE from "+StudentAttendance.DH_TABLE_NAME,null);
//
//        if (res.moveToFirst()){
//
//            GlobalFunction.printTextByTag("Attendance list cleared");
//
//        }
//        res.close();


    }


    public void clearAllCourse(String date){

        SQLiteDatabase db=this.getReadableDatabase();

        Cursor res=db.rawQuery("DELETE from "+Course.DH_TABLE_NAME+" WHERE "+Course.course_dateTime+"=?",new String[]{date});

        if (res.moveToFirst()){
            GlobalFunction.printTextByTag("course list cleared");
        }
        res.close();


    }

    private static final int STUDENT_CLASS=0;

    private static final int STUDENT_NAME=1;

    private static final int STUDENT_Employee=2;

    public Student getCorrespondingStudent(String cardNo){
        Student record=null;
        SQLiteDatabase db=this.getReadableDatabase();

        Cursor res=db.query(Student.DH_TABLE_NAME,null,  Student.student_employeeCode + " = ?"   ,
                new String[]{cardNo},null,null,null);

        if (res.moveToFirst()){
            record  =new Student();

            record.name=res.getString(STUDENT_NAME);

            record.className=res.getString(STUDENT_CLASS);

            record.employeeID=res.getString(STUDENT_Employee);

            record.unratedKW=res.getString(4);

            record.kwName=res.getString(res.getColumnIndex(Student.student_kwName));

            // GlobalFunction.printTextByTag("search success");

        }else {

            GlobalFunction.printTextByTag("search fail");
        }
       res.close();

        return record;

    }

    public int getCorrespondingAttendanceList(String className,String date,String classTime){
        StudentAttendance record=null;
        SQLiteDatabase db=this.getReadableDatabase();

        Cursor res=db.query(StudentAttendance.DH_TABLE_NAME,null,  StudentAttendance.student_class + " = ? AND "+ StudentAttendance.attendance_date + " = ? AND "+
                        StudentAttendance.class_time + " = ? ",
                new String[]{className,date,classTime},null,null,null);



        ArrayList<StudentAttendance> list=new ArrayList<>();
        res.moveToFirst();

    while (!res.isAfterLast()){

        record  =new StudentAttendance();
        record.employeeID=res.getString(res.getColumnIndex(StudentAttendance.student_employeeCode));
        record.name=res.getString(res.getColumnIndex(StudentAttendance.student_name));
        record.gender=res.getString(res.getColumnIndex(StudentAttendance.student_gender));
        record.className=res.getString(res.getColumnIndex(StudentAttendance.student_class));

        list.add(record);

       //GlobalFunction.printTextByTag("employee:"+record.employeeID);
        res.moveToNext();

            }

        res.close();


        return list.size();


    }


    public ArrayList<StudentAttendance> getAllAttendance(String className,String time,String date){//推送考勤列表
        StudentAttendance record=null;
        SQLiteDatabase db=this.getReadableDatabase();


        Cursor res=db.query(StudentAttendance.DH_TABLE_NAME,null,  StudentAttendance.attendance_isPush + "=? AND "+ StudentAttendance.attendance_date + "=?",
                new String[]{Setting.defaultClassName,date},null,null,null);


//        Cursor res=db.query(StudentAttendance.DH_TABLE_NAME,null, null ,
//             null,null,null,null);
        ArrayList<StudentAttendance> list=new ArrayList<>();
        boolean tem=res.moveToFirst();
        if (!tem){
            GlobalFunction.printTextByTag("查询推送考勤表失败");
        }

        while (!res.isAfterLast()){

            record  =new StudentAttendance();

            record.name=res.getString(res.getColumnIndex(StudentAttendance.student_name));
            record.gender=res.getString(res.getColumnIndex(StudentAttendance.student_gender));
            record.className=res.getString(res.getColumnIndex(StudentAttendance.student_class));
            record.attendanceDate=res.getString(res.getColumnIndex(StudentAttendance.attendance_date));
            record.attendanceTime=res.getString(res.getColumnIndex(StudentAttendance.attendance_time));
            record.classTime=res.getString(res.getColumnIndex(StudentAttendance.class_time));
            record.employeeID=res.getString(res.getColumnIndex(StudentAttendance.student_employeeCode));

            list.add(record);



            res.moveToNext();

        }

        res.close();


        return list;


    }

    public ArrayList<StudentAttendance> getAllAttendance(){//推送考勤列表
        StudentAttendance record=null;
        SQLiteDatabase db=this.getReadableDatabase();


        Cursor res=db.query(StudentAttendance.DH_TABLE_NAME,null,  StudentAttendance.attendance_isPush + "=0 ",
                new String[]{},null,null,null);


//        Cursor res=db.query(StudentAttendance.DH_TABLE_NAME,null, null ,
//             null,null,null,null);
        ArrayList<StudentAttendance> list=new ArrayList<>();
        boolean tem=res.moveToFirst();
        if (!tem){
            GlobalFunction.printTextByTag("查询推送考勤表失败");
        }

        while (!res.isAfterLast()){

            record  =new StudentAttendance();

            record.name=res.getString(res.getColumnIndex(StudentAttendance.student_name));
            record.gender=res.getString(res.getColumnIndex(StudentAttendance.student_gender));
            record.className=res.getString(res.getColumnIndex(StudentAttendance.student_class));
            record.attendanceDate=res.getString(res.getColumnIndex(StudentAttendance.attendance_date));
            record.attendanceTime=res.getString(res.getColumnIndex(StudentAttendance.attendance_time));
            record.classTime=res.getString(res.getColumnIndex(StudentAttendance.class_time));
            record.employeeID=res.getString(res.getColumnIndex(StudentAttendance.student_employeeCode));
            record.remark=res.getString(res.getColumnIndex(StudentAttendance.student_remark));

            list.add(record);



            res.moveToNext();

        }

        res.close();


        return list;


    }



    public int getTotalStudentForClass(String className){
        StudentAttendance record=null;
        SQLiteDatabase db=this.getReadableDatabase();

        Cursor res=db.query(Student.DH_TABLE_NAME,null,  Student.student_class + " = ? AND " ,
                new String[]{className},null,null,null);

        ArrayList<StudentAttendance> list=new ArrayList<>();
        res.moveToFirst();

        while (!res.isAfterLast()){

            record  =new StudentAttendance();

            record.name=res.getString(res.getColumnIndex(StudentAttendance.student_name));
            record.gender=res.getString(res.getColumnIndex(StudentAttendance.student_gender));
            record.className=res.getString(res.getColumnIndex(StudentAttendance.student_class));

            list.add(record);


            res.moveToNext();

        }

        res.close();


        return list.size();


    }

    public boolean checkStudentAttend(String className,String date,String employeeID,String classTime){

        SQLiteDatabase db=this.getReadableDatabase();

        Cursor res=db.query(StudentAttendance.DH_TABLE_NAME,null, StudentAttendance.student_class + " = ? AND "+ StudentAttendance.attendance_date + " = ? AND "
                + StudentAttendance.student_employeeCode + " = ? AND " + StudentAttendance.class_time + " = ?" ,
                new String[]{className,date,employeeID,classTime},null,null,null);

       boolean stat= res.moveToFirst();
        res.close();


        return stat;



    }


    public boolean checkStudentExist(String className,String employeeID){

        SQLiteDatabase db=this.getReadableDatabase();

        Cursor res=db.query(Student.DH_TABLE_NAME,null, Student.student_class + " = ? AND "+
                       Student.student_employeeCode + " = ?",
                new String[]{className,employeeID},null,null,null);

        boolean stat= res.moveToFirst();
        res.close();


        return stat;



    }


//


//    public void insertRecord(CardRecord record){
//
//        SQLiteDatabase db=this.getWritableDatabase();
//        ContentValues contentValues=new ContentValues();
//         contentValues.put(DH_Card_No,record.CardNo);
//        contentValues.put(DH_Card_UsageCount,record.UsageCount);
//        contentValues.put(DH_Card_purchaseAmount,record.PurchaseAmount);
//        contentValues.put(DH_Card_remain,record.Remain);
//        contentValues.put(DH_Card_discountRate,record.DiscountRate);
//       // GlobalFunction.printText("tag","record"+" "+record.getUsageDate());
//        contentValues.put(DH_Card_Date,record.usageDate);
//        contentValues.put(DH_Card_DeviceNo,record.DeviceNo);
//        contentValues.put(DH_Card_Operator,record.Operator);
//        contentValues.put(DH_Card_ReportType,record.ReportType);
//        contentValues.put(DH_Card_ReportType,record.ReportType);
//        db.insert(DH_TABLE_NAME, null, contentValues);
//        db.close();
//
//        GlobalFunction.printText("tag","row:"+numberOfRows());
//    }

//        public int numberOfRows(){
//            SQLiteDatabase db = this.getReadableDatabase();
//            int numRows = (int) DatabaseUtils.queryNumEntries(db, DH_TABLE_NAME);
//
//            return numRows;
//        }

   void deleteItem(String itemNo){//删除商品列表的某个商品
//        SQLiteDatabase db=this.getWritableDatabase();
//
//
//     db.delete(MenuItemTB.DH_TABLE_NAME,MenuItemTB.item_ID+"=?",new String[]{itemNo});
//

    }


//    public ArrayList<CardRecord> getCardReport(String operator, int reportType) {
//        SQLiteDatabase db=this.getReadableDatabase();
//        Cursor res=db.rawQuery("select * from "+DH_TABLE_NAME+ " WHERE "+DH_Card_ReportType+" ="+ String.valueOf(reportType),null);
//        res.moveToFirst();
//        ArrayList<CardRecord> recordList=new ArrayList<>();
//
//        while (!res.isAfterLast()){
//            CardRecord record=new CardRecord(res.getString(res.getColumnIndex(DH_Card_No)))  ;
//            record.UsageCount=res.getInt(res.getColumnIndex(DH_Card_UsageCount));
//            record.PurchaseAmount=res.getInt(res.getColumnIndex(DH_Card_purchaseAmount));
//            record.Remain=res.getInt(res.getColumnIndex(DH_Card_remain));
//            record.DiscountRate=res.getString(res.getColumnIndex(DH_Card_discountRate));
//            record.usageDate=res.getString(res.getColumnIndex(DH_Card_Date));
//            record.DeviceNo=res.getString(res.getColumnIndex(DH_Card_DeviceNo));
//            record.Operator=res.getString(res.getColumnIndexOrThrow(DH_Card_Operator));
//            record.ReportType=res.getInt(res.getColumnIndex(DH_Card_ReportType));
//            record.cardName=res.getString(res.getColumnIndex(DH_Card_Name));
//
//
//            recordList.add(record);
//
//
//
//            res.moveToNext();
//
//        }
//        res.close();
//
//        return recordList;
//
//    }

//    public String getPurchaseTotalAmount(String date){
//       int total_price=0;
//        GlobalFunction.printTextByTag(date);
//        SQLiteDatabase db=this.getReadableDatabase();
//        String query="select sum("+PurchasedItemTB.item_totalPrice+")"+" from "+PurchasedItemTB.DH_TABLE_NAME+" where "
//                +PurchasedItemTB.item_purchDate+" = " + "\""+date+"\"";
//        Cursor res=db.rawQuery(query,null);
//
//
//        GlobalFunction.printTextByTag(query);
//        if (res.moveToFirst()){
//
//            total_price=res.getInt(0);
//
//        }
//       return String.valueOf(total_price);
//
//    }

//    public ArrayList<PurchasedItemTB.Item> sumUpPurchaseGoodAmount(String date, int paymodeType){
//        SQLiteDatabase db=this.getReadableDatabase();
//        String query="select sum("+PurchasedItemTB.item__amount+")"+" as \"Total\""+","+PurchasedItemTB.item_name+","+PurchasedItemTB.item_ID+","+PurchasedItemTB.item_SaleCost
//        +" from "+PurchasedItemTB.DH_TABLE_NAME +" where " +PurchasedItemTB.item_purchDate+" = " + "\""+date+"\"" +" and "+PurchasedItemTB.item_payMode+"="+ String.valueOf(paymodeType) +" GROUP BY "+"\""+PurchasedItemTB.item_name+"\"";
//
//        Cursor res=db.rawQuery(query,null);
//
//      ArrayList<PurchasedItemTB.Item> recordList=new ArrayList<>();
//
//
//        if (res.moveToFirst()){
//
//          GlobalFunction.printTextByTag("query success");
//
//
//            while (!res.isAfterLast()){
//
//                PurchasedItemTB.Item item=new PurchasedItemTB.Item(res.getString(res.getColumnIndex(PurchasedItemTB.item_ID)));
//                item.item_name=res.getString(res.getColumnIndex(PurchasedItemTB.item_name));
//                item.item_amount=res.getInt(res.getColumnIndex("Total"));
//                item.saleCost=res.getInt(res.getColumnIndex(PurchasedItemTB.item_SaleCost));
//
//                recordList.add(item);
//
//
//
//                res.moveToNext();
//
//            }
//
//        }else {
//            GlobalFunction.printTextByTag("query fail");
//        }
//        res.close();
//
//        return recordList;
//
//
//    }





//    ArrayList<PurchasedItemTB.Item> getPurchasedItemHistory() {
//        SQLiteDatabase db=this.getReadableDatabase();
//        Cursor res=db.rawQuery("select * from "+ PurchasedItemTB.DH_TABLE_NAME ,null);
//        res.moveToFirst();
//        ArrayList<PurchasedItemTB.Item> recordList=new ArrayList<>();
//
//        while (!res.isAfterLast()){
//
//            PurchasedItemTB.Item item=new PurchasedItemTB.Item(res.getString(res.getColumnIndex(PurchasedItemTB.item_ID)));
//            item.card_ID=res.getString(res.getColumnIndex(PurchasedItemTB.item_CardID));
//            item.item_TicketID=res.getString(res.getColumnIndex(PurchasedItemTB.item_TicketID));
//            item.item_name=res.getString(res.getColumnIndex(PurchasedItemTB.item_name));
//            item.item_amount=res.getInt(res.getColumnIndex(PurchasedItemTB.item__amount));
//            item.item_payMode=res.getInt(res.getColumnIndex(PurchasedItemTB.item_payMode));
//            item.saleCost=res.getInt(res.getColumnIndex(PurchasedItemTB.item_SaleCost));
//            item.item_purchDate=res.getString(res.getColumnIndex(PurchasedItemTB.item_purchDate));
//            recordList.add(item);
//
//
//
//            res.moveToNext();
//
//        }
//        res.close();
//
//        return recordList;
//
//    }

//    ArrayList<PurchasedItemTB.Item> getPurchasedItemHistoryBaseOnPurchaseType(Integer type) {
//        SQLiteDatabase db=this.getReadableDatabase();
//        Cursor res=db.rawQuery("select * from "+ PurchasedItemTB.DH_TABLE_NAME+" WHERE " + PurchasedItemTB.item_payMode+" ="+ String.valueOf(type) ,null);
//
//
//
//        res.moveToFirst();
//        ArrayList<PurchasedItemTB.Item> recordList=new ArrayList<>();
//
//        while (!res.isAfterLast()){
//
//            PurchasedItemTB.Item item=new PurchasedItemTB.Item(res.getString(res.getColumnIndex(PurchasedItemTB.item_ID)));
//            item.card_ID=res.getString(res.getColumnIndex(PurchasedItemTB.item_CardID));
//            item.item_TicketID=res.getString(res.getColumnIndex(PurchasedItemTB.item_TicketID));
//            item.item_name=res.getString(res.getColumnIndex(PurchasedItemTB.item_name));
//            item.item_amount=res.getInt(res.getColumnIndex(PurchasedItemTB.item__amount));
//            item.item_payMode=res.getInt(res.getColumnIndex(PurchasedItemTB.item_payMode));
//            item.saleCost=res.getInt(res.getColumnIndex(PurchasedItemTB.item_SaleCost));
//            item.item_purchDate=res.getString(res.getColumnIndex(PurchasedItemTB.item_purchDate));
//            recordList.add(item);
//
//
//
//            res.moveToNext();
//
//        }
//        res.close();
//
//        return recordList;
//
//    }


//      ArrayList<RefundGoodTB.Item> getRefundedItemList(PurchasedItemTB.Item purchaseItem) {
//        SQLiteDatabase db=this.getReadableDatabase();
//        Cursor res=db.query(RefundGoodTB.DH_TABLE_NAME,null,  RefundGoodTB.item_TicketID + " = ?" +" AND "+RefundGoodTB.item_ID+" = ?"+ " AND "+RefundGoodTB.item_payMode+
//                 " = ?", new String[]{purchaseItem.item_TicketID,purchaseItem.item_ID, String.valueOf(purchaseItem.item_payMode) },null,null,null);
//
//        res.moveToFirst();
//
//        ArrayList<RefundGoodTB.Item> recordList=new ArrayList<>();
//        while (!res.isAfterLast()){
//
////    GlobalFunction.printTextByTag("query "+purchaseItem.item_TicketID+" "+purchaseItem.item_ID);
//
//
//            RefundGoodTB.Item item=  new RefundGoodTB.Item(res.getString(res.getColumnIndex(RefundGoodTB.item_ID)));
//           // item.card_ID=res.getString(res.getColumnIndex(PurchasedItemTB.item_CardID));
//            item.item_TicketID=res.getString(res.getColumnIndex(RefundGoodTB.item_TicketID));
//            item.item_name=res.getString(res.getColumnIndex(RefundGoodTB.item_name));
//            item.item_amount=res.getInt(res.getColumnIndex(RefundGoodTB.item__amount));
//            item.item_payMode=res.getInt(res.getColumnIndex(RefundGoodTB.item_payMode));
//            item.saleCost=res.getInt(res.getColumnIndex(RefundGoodTB.item_SaleCost));
//            item.item_purchDate=res.getString(res.getColumnIndex(RefundGoodTB.item_purchDate));
//            item.item_refundDate=res.getString(res.getColumnIndex(RefundGoodTB.item_refundDate));
//
//            recordList.add(item);
//
//            res.moveToNext();
//
//        }
//
//        res.close();
//
//        return recordList;
//
//    }

//    ArrayList<RefundGoodTB.Item> getAllRefundedItemListBaseOnType(int type){
//        SQLiteDatabase db=this.getReadableDatabase();
//        Cursor res=db.rawQuery("select * from "+ RefundGoodTB.DH_TABLE_NAME+" WHERE " + RefundGoodTB.item_payMode+" ="+ String.valueOf(type) ,null);
//
//
//
//        res.moveToFirst();
//        ArrayList<RefundGoodTB.Item> recordList=new ArrayList<>();
//
//        while (!res.isAfterLast()){
//
//            RefundGoodTB.Item item=new RefundGoodTB.Item(res.getString(res.getColumnIndex(PurchasedItemTB.item_ID)));
//            item.card_ID=res.getString(res.getColumnIndex(PurchasedItemTB.item_CardID));
//            item.item_TicketID=res.getString(res.getColumnIndex(PurchasedItemTB.item_TicketID));
//            item.item_name=res.getString(res.getColumnIndex(PurchasedItemTB.item_name));
//            item.item_amount=res.getInt(res.getColumnIndex(PurchasedItemTB.item__amount));
//            item.item_payMode=res.getInt(res.getColumnIndex(PurchasedItemTB.item_payMode));
//            item.saleCost=res.getInt(res.getColumnIndex(PurchasedItemTB.item_SaleCost));
//            item.item_purchDate=res.getString(res.getColumnIndex(PurchasedItemTB.item_purchDate));
//            item.item_refundDate=res.getString(res.getColumnIndex(RefundGoodTB.item_refundDate));
//
//            recordList.add(item);
//
//
//
//            res.moveToNext();
//
//        }
//        res.close();
//
//
//    return recordList;
//
//    }

//   public int getCardReportRefundIndex(String cardID){
//        SQLiteDatabase db=this.getReadableDatabase();
//        Cursor res=db.rawQuery("select * from "+DH_TABLE_NAME+ " WHERE "+DH_Card_No+" = "+cardID,null);
//        res.moveToFirst();
//
//        CardRecord record=null;
//
//        while (!res.isAfterLast()){
//            record=new CardRecord(res.getString(res.getColumnIndex(DH_Card_No)))  ;
//            record.UsageCount=res.getInt(res.getColumnIndex(DH_Card_UsageCount));
//            record.PurchaseAmount=res.getInt(res.getColumnIndex(DH_Card_purchaseAmount));
//            record.Remain=res.getInt(res.getColumnIndex(DH_Card_remain));
//            record.DiscountRate=res.getString(res.getColumnIndex(DH_Card_discountRate));
//            record.usageDate=res.getString(res.getColumnIndex(DH_Card_Date));
//            record.DeviceNo=res.getString(res.getColumnIndex(DH_Card_DeviceNo));
//            record.Operator=res.getString(res.getColumnIndexOrThrow(DH_Card_Operator));
//            record.ReportType=res.getInt(res.getColumnIndex(DH_Card_ReportType));
//            record.cardName=res.getString(res.getColumnIndex(DH_Card_Name));
//            record.refundIndex=res.getInt(res.getColumnIndex(DH_Card_RefundIndex));
//
//
//
//
//
//            res.moveToNext();
//
//        }
//        res.close();
//
//        return record.refundIndex;
//
//
//
//    }

// public    void updateCardRefundIndex(CardRecord cardRecord){
//         SQLiteDatabase db=this.getReadableDatabase();
//       cardRecord.setRefundIndex(1);//表示退过款
//
//    String query="UPDATE "+DH_TABLE_NAME+ " SET "+ DH_Card_RefundIndex + "=1"+" WHERE "+DH_Card_No+"="+cardRecord.getCardNo();
//    db.execSQL(query);
//db.close();
//GlobalFunction.printTextByTag(cardRecord.getCardNo());
//    }



//    ArrayList<Goods> getAllMenuItem(){//获取商品列表所有商品
//
//        ArrayList<Goods> itemArrayList=new ArrayList<>();
//
//        SQLiteDatabase db=this.getReadableDatabase();
//        Cursor res=db.rawQuery("select * from "+ MenuItemTB.DH_TABLE_NAME ,null);
//        res.moveToFirst();
//
//        while (!res.isAfterLast()){
//
//              Goods item=new     Goods();
//            item.setItemNo(res.getString(res.getColumnIndex(MenuItemTB.item_ID)));
//            item.setItemName(res.getString(res.getColumnIndex(MenuItemTB.item_ID)));
//            item.setAmount(res.getInt(res.getColumnIndex(MenuItemTB.item_amount)));
//            item.setRetailPrice(res.getInt(res.getColumnIndex(MenuItemTB.item_retailPrice)));
//
//
//             itemArrayList.add(item);
//
//        res.moveToNext();
//
//        }
//        res.close();
//
//
//        return itemArrayList;
//
//    }


}
